<template>
  <div>
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input placeholder="请输入企业名称" class="search-main" clearable @clear="getList" v-model="params.enterpriseName" />
      <el-button style="margin-left: 30px" type="primary" @click="getList">查询</el-button>
    </div>
    <el-button class="btu" type="primary" size="medium" @click="open">添加账单</el-button>
    <!--    表格区域-->
    <div class="table">
      <el-table style="width: 100%" :data="list">
        <el-table-column type="index" label="序号" width="70" />
        <el-table-column label="账单编号" prop="billNumber" />
        <el-table-column label="企业名称" prop="enterpriseName" />
        <el-table-column label="租赁楼宇" prop="buildingName" />
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" />
        <el-table-column label="账单金额(元)" prop="paymentAmount" />
        <el-table-column label="缴费时间" prop="createTime" />
        <el-table-column label="操作" fixed="right">
          <template #default="scope">
            <el-button size="mini" type="text" @click="look(scope.row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="box">

      <el-pagination :current-page="params.page" :page-sizes="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
        :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    </div>

    <el-dialog title="添加账单" :visible="flag" @close="handleClose" width="33%">
      <div style="display: flex;justify-content: center">

        <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="90px" style="width: 400px;">
          <el-form-item label="选择租户:" prop="enterpriseId">
            <el-select v-model="form.enterpriseId" style="width: 360px;">
              <el-option v-for="item in enterpriseList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="租赁楼宇:" prop="buildingId">
            <el-select v-model="form.buildingId" style="width: 360px;">
              <el-option v-for="item in buildingList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="缴费周期:" prop="timeRange2">
            <el-date-picker style="width: 360px;" v-model="timeRange2" type="daterange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="支付金额:" prop="paymentAmount">
            <el-input v-model="form.paymentAmount" style="width: 360px;" :disabled="true">{{ getPrice
            }}</el-input>
          </el-form-item>

          <el-form-item label="支付方式:" prop="paymentMethod">
            <el-select v-model="form.paymentMethod" style="width: 360px;">
              <el-option v-for="item in payMethodList" :key="item.id" :label="item.type" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleClose">取消</el-button>
            <el-button @click="confirm" type="primary">确认</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <el-dialog title="查看账单" :visible="flag1" @close="handleClose" width="580px">
      <div class="dialog-box ">
        <p>
          租户名称:<span>{{ this.form.enterpriseName }}</span>
        </p>
        <p>
          租赁楼宇：<span>{{ this.form.buildingName }}</span>
        </p>
        <p>
          缴费周期：<span>{{ this.form.timeRange }}</span>
        </p>
        <p>
          物业费(元/m²)<span>{{ this.form.propertyFeePrice }}</span>
        </p>
        <p>
          账单金额(元)<span>{{ this.form.paymentAmount }}</span>
        </p>
        <p>
          支付方式：<span>{{ this.form.paymentMethodValue }}</span>
        </p>
        <p>
          缴费时间：<span>{{ this.form.createTime }}</span>
        </p>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="flag1 = false">取 消</el-button>
        <el-button type="primary" @click="flag1 = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  getExpensesAPI, addExpensesAPI,
  enterpriseAPI, buildingAPI,
  budgetAPI, getPropertyfeeAPI,
  delExpensesAPI
} from "@/api/propety";

export default {
  name: "propety",
  data() {
    var validatePass = (rule, value, callback) => {
      if (this.timeRange2 === '') {
        callback(new Error('必须填写'));
      } else {
        callback();
      }
    };
    return {
      list: [],
      enterpriseList: [],
      buildingList: [],
      total: '',
      timeRange: '',
      timeRange2: '',
      flag: false,
      flag1: false,
      params: {
        enterpriseName: '',
        start: '',
        end: '',
        page: 1,
        pageSize: 10,
      },
      form: {
        enterpriseId: '',
        buildingId: "",
        startTime: "",
        endTime: "",
        paymentAmount: "",
        paymentMethod: "",
      },

      payMethodList: [
        {
          id: 1,
          type: '微信'
        }, {
          id: 2,
          type: '支付宝'
        }, {
          id: 3,
          type: '现金'
        }
      ],

      rules: {
        enterpriseId: [
          { required: true, message: '必须填写', trigger: 'blur' },
        ],
        buildingId: [
          { required: true, message: '必须填写', trigger: 'blur' },
        ],
        timeRange2: [
          { validator: validatePass, required: true, trigger: 'blur' },
        ],
        paymentAmount: [
          { required: true, message: '必须填写', trigger: 'blur' },
        ],
        paymentMethod: [
          { required: true, message: '必须填写', trigger: 'blur' },
        ]
      },
    }
  },
  async mounted() {
    this.getList()
    let res = await enterpriseAPI();
    this.enterpriseList = res.data;
    res = await buildingAPI();
    this.buildingList = res.data

  },
  computed: {
    async getPrice() {
      let price = ''
      if (this.timeRange2 !== '' && this.form.buildingId !== '') {
        const res = await budgetAPI({
          buildingId: this.form.buildingId,
          startTime: this.timeRange2[0],
          endTime: this.timeRange2[1]
        })
        this.form.paymentAmount = res.data
        console.log("99", this.form.paymentAmount)
        console.log(this.timeRange2)
      }
      return price
    }
  },
  methods: {

    //获取数据
    getList() {
      if (this.timeRange !== '') {
        this.params.start = this.timeRange[0]
        this.params.end = this.timeRange[1]
      }

      getExpensesAPI(this.params).then(res => {
        this.list = res.data.rows
        this.total = res.data.total
      })
    },
    //打开弹窗
    open() {
      this.flag = true
    },
    //关闭
    handleClose() {
      this.flag1 = false
      this.flag = false
      this.form = {
        enterpriseId: '',
        buildingId: "",
        startTime: "",
        endTime: "",
        paymentAmount: "",
        paymentMethod: "",
      }
      this.timeRange2 = ''
    },
    //添加
    confirm() {
      this.form.startTime = this.timeRange2[0];
      this.form.endTime = this.timeRange2[1]
      this.$refs.form.validate((ok) => {
        if (ok) {
          addExpensesAPI(this.form).then(res => {
            this.handleClose()
            this.getList()
            this.$message.success("添加成功")
          })
        }
      })

    },
    //查看
    async look(id) {
      const res = await getPropertyfeeAPI(id);
      console.log(res)
      this.form.enterpriseName = res.data.enterpriseName
      this.form.buildingName = res.data.buildingName
      this.form.timeRange = res.data.startTime + '至' + res.data.endTime
      this.form.propertyFeePrice = res.data.propertyFeePrice
      this.form.paymentAmount = res.data.paymentAmount
      this.form.paymentMethodValue = res.data.paymentMethodValue
      this.form.createTime = res.data.createTime
      this.flag1 = true
    },


    handleCurrentChange(page) {
      console.log('页码变了', page)
      this.params.page = page  // 更新页码
      this.getList()
    },
    handleSizeChange(pageSize) {
      console.log('每页条数变了', pageSize)
      this.params.pageSize = pageSize
      // this.params.page = 1
      this.getList()
    },
    //删除      
    del(id) {
      this.$confirm('确定删除该数据？').then(() => {
        delExpensesAPI(id).then(res => {
          this.$message.success("删除成功")
          this.getList()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-box {
  padding-left: 40px;

  p {
    font-size: 16px;
    color: #909399;
    line-height: 20px;
    margin-bottom: 30px;

    span {
      color: #262626;
      margin-left: 10px;
    }
  }
}

.box {
  float: right;
  margin-top: 10px;
  margin-right: 20px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
</style>